package com.gxg.controller;

import com.gxg.entities.StudentUser;
import com.gxg.entities.User;
import com.gxg.services.MD5Service;
import com.gxg.services.UserService;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController
{
  @Autowired
  private UserService userService;
  @Autowired
  private MD5Service md5Service;
  
  @PostMapping({"/login"})
  @ResponseBody
  public String login(@RequestParam("id") String id, @RequestParam("password") String password, @RequestParam("checkCode") String checkCode, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("checkCode") == null) {
      return "随机验证码出错！";
    }
    if (session.getAttribute("checkCode").equals(checkCode))
    {
      String result = this.userService.validateLogon(id, password);
      if (result.equals("ok"))
      {
        User user = this.userService.getUser(id);
        session.setAttribute("user", user);
      }
      else if ("ok:student".equals(result))
      {
        User user = new User();
        StudentUser studentUser = this.userService.getStudentUserById(id);
        user.setName(studentUser.getStudentName());
        user.setRole("学生");
        user.setId(studentUser.getStudentId());
        user.setTel(studentUser.getTel());
        user.setPassword(this.md5Service.md5(studentUser.getPassWord()));
        user.setEmail("学生账号没有邮箱");
        user.setSex("学生账号没有性别");
        session.setAttribute("user", user);
      }
      return this.userService.validateLogon(id, password);
    }
    return "验证码错误！";
  }
  
  @PostMapping({"/get_user"})
  @ResponseBody
  public String getUser(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "error:用户没有登录！";
    }
    User user = (User)session.getAttribute("user");
    return user.toString();
  }
  
  @RequestMapping({"/sign_out"})
  @ResponseBody
  public String signOut(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    session.setAttribute("user", null);
    return "ok";
  }
  
  @PostMapping({"/keep_user_connection"})
  @ResponseBody
  public String keepUserConnection(HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "error";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return "ok";
  }
  
  @PostMapping({"/get_teacher_info"})
  @ResponseBody
  public String getTeacherInfo(@RequestParam("teacherId") String teacherId, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") != null)
    {
      User user = (User)session.getAttribute("user");
      session.setAttribute("user", user);
    }
    return this.userService.getUser(teacherId).toString();
  }
  
  @PostMapping({"/reset_password"})
  @ResponseBody
  public String resetPassword(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("email") String email, @RequestParam("tel") String tel, @RequestParam("newPassword") String newPassword, @RequestParam("repeatPassword") String repeatPassword)
  {
    return this.userService.resetPassword(id, name, email, tel, newPassword, repeatPassword);
  }
  
  @PostMapping({"/add_user"})
  @ResponseBody
  public String addUser(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("sex") String sex, @RequestParam("email") String email, @RequestParam("tel") String tel, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.userService.addUser(id, name, sex, email, tel, user);
  }
  
  @PostMapping({"/change_password"})
  @ResponseBody
  public String changePassword(@RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword, @RequestParam("confirmPassword") String confirmPassword, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.userService.changePassword(oldPassword, newPassword, confirmPassword, user);
  }
  
  @PostMapping({"/change_user_information"})
  @ResponseBody
  public String changeUserInformation(@RequestParam("userPassword") String userPassword, @RequestParam("userName") String userName, @RequestParam("userSex") String userSex, @RequestParam("userTel") String userTel, @RequestParam("userEmail") String userEmail, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    if (session.getAttribute("user") == null) {
      return "没有用户！";
    }
    User user = (User)session.getAttribute("user");
    session.setAttribute("user", user);
    return this.userService.changeUserInformation(userPassword, userName, userSex, userTel, userEmail, user);
  }
  
  @PostMapping({"/add_student_user"})
  @ResponseBody
  public String getStudentUser(@RequestParam("studentId") String studentId, @RequestParam("teacherName") String teacherName, @RequestParam("studentName") String studentName, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    User user = (User)session.getAttribute("user");
    if (!"教师".equals(user.getRole())) {
      return "error:没有权限添加用户";
    }
    StudentUser studentUser = new StudentUser();
    studentUser.setClassId("0");
    studentUser.setPassWord(studentId);
    studentUser.setStudentId(studentId);
    studentUser.setteacherName(teacherName);
    studentUser.setTel("13888888888");
    studentUser.setStudentName(studentName);
    studentUser.setTimestamp(new Timestamp(System.currentTimeMillis()));
    String msg = this.userService.addStudentUser(studentUser);
    return msg;
  }
}
